<?php
/**
 * Description of historiqueIncident
 *
 * @author michael
 */
class historiqueIncident {
    private $incident_id = null;
    private $changements = array();
    
    public function __construct($id) {
        $this->incident_id = $id;
        $this->getAllChangements();
    }
    private function getAllChangements(){
        $incident = MlIncidentsTable::getInstance()->find($this->incident_id);
        $last_version = $incident->get('version');
        $test = array(  'intervention'      => 'Intervention',
                        'solution'          => 'Solution',
                        'pieces_remplaces'  => 'Pièces remplacées',
                        'controler'         => 'A contrôler',
                        'sfm'               => 'Intervention SFM',
                        'controler_do'      => 'Contrôlé',
                        'sfm_do'            => 'SFM intervenu',
                        'priorite'          => 'Etat',
                        'intervention_date' => 'Date d\'intervention');

        for($i = ($last_version - 1); $i > 0; $i-- ){
            $user = sfGuardUserTable::getInstance()->find($incident->getUpdatedBy());
            foreach($test AS $to_test => $label){
                $method = 'get' . sfInflector::camelize($to_test);
                $date = $incident->getDateTimeObject('updated_at')->format('d/m/Y H:i');
                $new = $this->getEtatCheckbox( $to_test == 'intervention_date' ? $incident->getDateTimeObject($to_test)->format('d/m/Y H:i') :$incident->$method() );
                $old = $this->getEtatCheckbox( $to_test == 'intervention_date' ? $incident->revert($i)->getDateTimeObject($to_test)->format('d/m/Y H:i') :$incident->revert($i)->$method() );
                if($new != $old){
                    $this->changements[$i][] = array( 'date'      => $date,
                                                    'user'      => $user->getLastName(),
                                                    'label'     => $label,
                                                    'changement'=> $old . ' > ' . $new);
                }
                $incident->revert($i+1);
            }
            $date = $incident->getDateTimeObject('updated_at')->format('d/m/Y H:i');
            $new = sfGuardUserTable::getInstance()->find($incident->getPresenceId());
            $old = sfGuardUserTable::getInstance()->find($incident->revert($i)->getPresenceId());
            if($new->getLastName() != $old->getLastName()){
                $this->changements[$i][] = array( 'date'      => $date,
                                                'user'      => $user->getLastName(),
                                                'label'     => 'Personne présente',
                                                'changement'=> $old->getLastName() . ' > ' . $new->getLastName());
            }
            $incident->revert($i+1);            
        }
    }
    private function getEtatCheckbox($bool){
        if(is_bool($bool)){
            return $bool ? 'coché' : 'décoché';
        }
        return $bool;
    }
    public function getChangementsAsArray(){
        return $this->changements;
    }
}

?>
